package com.sunseen.intelligencebus

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import org.greenrobot.eventbus.EventBus

class BluetoothStateChangeReceiver : BroadcastReceiver() {


    override fun onReceive(context: Context, intent: Intent) {
        Log.e("TAG", "onReceive---------" + intent.action)
        when (intent.action) {
            BluetoothAdapter.ACTION_STATE_CHANGED -> {
                val blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)
                when (blueState) {
                    BluetoothAdapter.STATE_TURNING_ON -> Log.e("TAG", "onReceive---------STATE_TURNING_ON")
                    BluetoothAdapter.STATE_ON -> {
                        EventBus.getDefault().post(BluetoothStateChangeEvent(BluetoothAdapter.STATE_ON))
                        Log.e("TAG", "onReceive---------STATE_ON")
                    }
                    BluetoothAdapter.STATE_TURNING_OFF -> Log.e("TAG", "onReceive---------STATE_TURNING_OFF")
                    BluetoothAdapter.STATE_OFF -> {
                        EventBus.getDefault().post(BluetoothStateChangeEvent(BluetoothAdapter.STATE_OFF))
                        Log.e("TAG", "onReceive---------STATE_OFF")
                    }
                }//Ble.toReset(mContext);
                //Ble.toReset(mContext);
            }
            BluetoothDevice.ACTION_ACL_CONNECTED -> {
                val device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) as BluetoothDevice
                EventBus.getDefault().post(ConnectedEvent(device.address))
            }
            BluetoothDevice.ACTION_ACL_DISCONNECTED -> {

            }
        }
    }
}
